package com.loong.game2048.server.impl;

import com.loong.game2048.dao.ForumDao;
import com.loong.game2048.dao.PictureDao;
import com.loong.game2048.dao.UserDao;
import com.loong.game2048.module.Forum;
import com.loong.game2048.module.Picture;
import com.loong.game2048.module.User;
import com.loong.game2048.module.vo.ForumVo;
import com.loong.game2048.module.vo.PaginationVo;
import com.loong.game2048.server.ForumServer;
import com.loong.game2048.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ForumServerImpl implements ForumServer {

    @Resource
    private UserDao userDao;

    @Resource
    private ForumDao forumDao;

    @Resource
    private PictureDao pictureDao;


    @Override
    public boolean isExistUser(String uId) {
        User user = userDao.getUserById(uId);
        if (user == null)
        return false;
        return true;
    }

    @Override
    public Integer addForum(Forum forum) {
        forum.setTime(new Date());
        //html转义和敏感词拦截
        forum.setContent(StrUtils.HTMLEncod(forum.getContent()));
        forum.setTitle(StrUtils.HTMLEncod(forum.getTitle()));
        Integer f_id = forumDao.addForum(forum);
        return f_id;
    }



    @Override
    public PaginationVo list(Integer page, Integer size, Integer tag) {
        PaginationVo<ForumVo> paginationVo = new PaginationVo();
        Integer totalPage;

        Integer totalCount;
        if (tag == null){
            totalCount = forumDao.countAll();
        }else {
            totalCount = forumDao.countAllwithTag(tag);
        }

        if (totalCount % size == 0 ){
            totalPage = totalCount / size;
        }else {
            totalPage = totalCount / size + 1;
        }

        if (page < 1){
            page = 1;
        }
        if ( page > totalPage ) {
            page = totalPage;
        }
        //设置页数显示
        paginationVo.setPagination(totalPage,page);
        Integer offset = size * (page-1);

        List<Forum> forums;
        if (tag == null){
           forums = forumDao.list(offset,size);
        }else {
            forums = forumDao.listWithTag(offset,size,tag);
        }

        List<ForumVo> forumVos = new ArrayList<>();

        for (Forum forum : forums) {
            //查询帖子主人
            User user = userDao.getUserById(forum.getUid());
            //查询帖子图片
            List<String> pictures = pictureDao.findByFidOnlyName(forum.getId());
            //展示前3张
            if ( pictures!= null && pictures.size() > 3) {
                while (pictures.size() != 3){
                    pictures.remove(3);
                }
            }

            ForumVo forumVo = new ForumVo();
            BeanUtils.copyProperties(forum,forumVo);
            forumVo.setUser(user);
            forumVo.setPictures(pictures);
            forumVos.add(forumVo);
        }

        paginationVo.setDatas(forumVos);
        return paginationVo;
    }

    @Override
    public Forum findById(int id) {
        Forum forum = forumDao.findById(id);
        return forum;
    }

    @Override
    public void delForum(Integer id) {
        forumDao.delete(id);
    }

    @Override
    public void update(Forum forum) {
        forumDao.update(forum);
    }
}
